home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / WebFace / Source / 10-InfoSystems / mail / pop-account.frm.z / pop-account.frm
Encoding:
Text File  |  1997-07-30  |  8.9 KB  |  308 lines

  1. #!/usr/bin/perl5
  2. #
  3. # pop-account.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: pop-account.frm,v 1.19 1997/06/19 22:25:46 shotes Exp $
  21.  
  22. require "/usr/OnRamp/lib/OnRamp.pm";
  23.  
  24. $conf = "/etc/passwd";
  25. $dummy = "/etc/passwd.temp";
  26. $myname = "pop-account.cgi";
  27. $title = "POP3 Mail Accounts";
  28. $popdir = "/usr/pop3";
  29.  
  30. $js_main =
  31. "which = \"none\";
  32. function runSubmit() {
  33.     // onClick gets processed before onSubmit
  34.     if(which == \"add\") return runAdd();
  35.     if(which == \"delete\") return runDelete();
  36.     return (true);
  37. }    
  38. function markAdd() { which = \"add\"; }
  39. function markDelete() { which = \"delete\"; }
  40. function markOther() { which = \"none\"; }
  41. function runAdd()  {
  42.     form = document.POPForm;
  43.     if (!testLogin(form)) return (false);
  44.     return (true);
  45. }
  46. function runDelete()  {
  47.     Ctrl = document.POPForm.chosen;
  48.     none = true;
  49.     for(i = 0; i < Ctrl.length; i++) { if (Ctrl.options[i].selected) { none = false; break; } }
  50.     if(none) { errorBox (Ctrl, \"No account selected.\"); return (false); }
  51.     return (true);
  52. }
  53. function testLogin(form) {
  54.     Ctrl = form.new_account;
  55.     if (Ctrl.value == \"\") { errorBox (Ctrl, \"To add a new account, first enter a login name for \\nthe account, then click the \\\"Add New Account\\\" button.\"); return (false); } 
  56.     error = testLoginChars(Ctrl.value); 
  57.     if (error == 1) { errorBox (Ctrl, \"The login name cannot be more \\nthan 8 characters long.\"); return (false); }
  58.     if (error == 2) { errorBox (Ctrl, \"The login name cannot contain the \" + illegal + \" character.\"); return (false); }
  59.     if (error == 3) { errorBox (Ctrl, \"The login name cannot contain spaces.\"); return (false); }
  60.      return (true);
  61. }
  62. function testLoginChars(word) {
  63.     if (word.length > 8) return 1;
  64.     loginChars = \"_-\.{}()*!~<>?|[]'&^\$\@\#`\\\";:\\\\/+=,%\";
  65.     for(j = 0; j < loginChars.length; j++) {
  66.         c = loginChars.charAt(j);
  67.         if (word.indexOf(c, 0) != -1) { illegal = c; return 2; }
  68.     }
  69.     for (c = 0; c < word.length; c++) { if (word.charAt(c) == ' ') return 3; }
  70.     return 0;
  71. }
  72. function errorBox (Ctrl, ErrorMessage) {
  73.     alert (ErrorMessage);  Ctrl.focus();  return;
  74. }";
  75.  
  76.  
  77. $js_add =
  78. "which = \"none\";
  79. function runSubmit() {
  80.     // onClick must get processed before onSubmit
  81.     if(which == \"add\") return runAdd();
  82.     return (true);
  83. }    
  84. function markAdd() { which = \"add\"; }
  85. function markOther() { which = \"none\"; }
  86. function runAdd()  {
  87.     form = document.AddForm;
  88.     // JavaScript can't look at password fields.
  89.     return (true);
  90. }
  91. function errorBox (Ctrl, ErrorMessage) {
  92.     alert (ErrorMessage);  Ctrl.focus();  return;
  93. }";
  94.  
  95. print "Content-type: text/html\n\n";
  96.  
  97. &get_fields;
  98.  
  99. &getAccounts;
  100.  
  101. if (%fld) {
  102.     $fld{'chosen'} =~ /([\w.-]+)/;
  103.     $fld{'chosen'} = $1;
  104.  
  105.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  106.     $help =~ s/cgi$/hlp/;
  107.     exec $help if ($fld{'help'} eq "Help");
  108.  
  109.     if ($fld{'add'}) { 
  110.         &formValid_add;
  111.         @test = getpwnam($fld{'new_account'});
  112.         &error(2,"Account already exists.") if $test[0];
  113.  
  114.         $val{'login'} = $fld{'new_account'};
  115.         &addAccount; 
  116.     }
  117.     elsif ($fld{'delete'}) { 
  118.         &error(2,"No account selected.") if !$fld{'chosen'};
  119.         $message = qq|Click "Ok" to save changes.|;
  120.         &generic($fld{'chosen'}); 
  121.     }
  122.     elsif ($fld{'doit'}) { 
  123.         &tryToDelete; 
  124.         &getAccounts; 
  125.         if (!$message) { $message = "No changes made."; }
  126.         &generic; 
  127.     }
  128.     elsif ($fld{'doadd'}) { &formValid_doAdd; &doAdd; &getAccounts; &generic; }
  129.     else { $message = "Use buttons to submit form."; &generic; } 
  130.     
  131. } else { 
  132.     $NSstatus = &checkForNS;
  133.  
  134.     if ($NSstatus) { 
  135.         &title_block($title);
  136.         &header_block($title);
  137.         print "<i>NS Mail is currently running.  To enable POP3 Mail, first
  138.                disable NS Mail.</i>";
  139.     } else {
  140.         &generic;
  141.     }
  142. }
  143.  
  144. sub checkForNS {
  145.     local($mail_process) = "NscpMail";
  146.     local($ret) = 0;
  147.     open(PSNS, "/bin/ps -ef |");
  148.     while (<PSNS>) {
  149.     if ($_ =~ m:$mail_process:) { $ret = 1; }
  150.     }
  151.     close(PSNS);
  152.     $ret;
  153. }
  154.  
  155. sub formValid_add {
  156.     &error(1,qq|To add new account, first enter in login name for new account, 
  157.         then click "Add New Account" button.|) if !$fld{'new_account'};
  158.     &error(1,qq|Invalid login name.|) if $fld{'new_account'} =~ /$LOGINCHARS/o;
  159. }
  160.  
  161. sub formValid_doAdd {
  162.     if (!$fld{'password'}) 
  163.     { &error(0,"You must enter a password."); }
  164.     if ($fld{'password'} ne $fld{'passcheck'}) 
  165.     { &error(0,"Input passwords not equivalent."); }
  166.     &error(0,"Invalid login name.") if $fld{'login'} =~ /$METACHARS/o;
  167. }
  168.  
  169. sub error {
  170.     &error_block($_[1]);
  171.     %val = %fld;
  172.     if ($_[0] == 0) { &addAccount; }
  173.     if ($_[0] == 1 || $_[0] == 2) { undef @_; &generic; }
  174.     exit 0;
  175. }
  176.  
  177. sub tryToDelete {
  178.     $account = $fld{'del_name'};
  179.     if ($account eq "") { return; }
  180.  
  181.     open(IN,"< $conf");
  182.     open(OUT,"> $dummy");
  183.     while(<IN>) {
  184.     $line = $_;
  185.     if ($line =~ /^\s*\#/) { print OUT $line; next; }
  186.     @items = split(/:/,$line);
  187.     if ( ($items[5] =~ /^$popdir/) && $items[0] eq $account) {
  188.         next;
  189.     } else { print OUT $line; }
  190.     }
  191.     close(IN);
  192.     close(OUT);
  193.     rename($dummy, $conf);
  194.  
  195.     $message = "Account deleted.";    
  196. }
  197.  
  198. sub doAdd {
  199.     local($lgname) = $fld{'login'};
  200.     $userdir = $popdir."/$lgname";
  201.     &add_password($lgname,$fld{'password'},2000,20,$lgname." pop3",
  202.         $userdir, "/bin/false");
  203.  
  204.     if (! -d $popdir) {
  205.     mkdir($popdir, 0755);
  206.     }
  207.     mkdir($userdir, 0755);
  208.     chown((getpwnam($lgname))[2,3], $userdir);
  209.     $message = "New account added.";
  210. }
  211.  
  212. sub addAccount {
  213.     &js_title_block($title,$js_add);
  214.     &header_block("Add New POP3 Mail Account");
  215.  
  216.     print "<form action=$myname method=post name=AddForm onSubmit=\"return runSubmit()\">\n";
  217.  
  218.     print "<center><table>";
  219.  
  220.     print "<input type=hidden name=login value=$val{'login'}>";
  221.  
  222.     print "<tr><th align=left>Login name:</th><td><tt>",
  223.       $val{'login'},"</tt></td></tr>";
  224.  
  225.     print "<tr><th align=left>Password:</th><th align=left>";
  226.     print qq|<input type=password name="password" size=20>|;
  227.     print "</th></tr>";
  228.  
  229.     print "<tr><th align=left>(Password again):</th><th align=left>";
  230.     print qq|<input type=password name="passcheck" size=20>|;
  231.     print "</th></tr>";
  232.  
  233.     print "</table></center><br>";
  234.  
  235.     print &js_buttons('doadd','Ok','onClick="markAdd()"','onClick="markOther()"');
  236.  
  237.     print "</form></body></html>";
  238. }
  239.  
  240. sub getAccounts {
  241.     $i = 0;
  242.     open(IN,"< $conf");
  243.     while(<IN>) {
  244.     $line = $_;
  245.     if ($line =~ /^\s*\#/) { next; }
  246.     @items = split(/:/,$line);
  247.     chop $items[5];
  248.     if ($items[5] =~ /^$popdir/) {
  249.         $name[$i] = $items[0];
  250.         $i++;
  251.     }
  252.     }
  253.     $number = $i;
  254.     close(IN);
  255. }
  256.  
  257. sub generic {
  258.         if (! -e "/usr/freeware/lib/popper") {  
  259.                 print "<center><h2>POP3 Mail Server</h2></center>\n",
  260.                         "<i>POP3 Mail Server software is not installed on this system",
  261.                         " by default.  Please install the </i><b>fw_BSDqpopper</b><i>",
  262.                         " from the freeware CD.</i>";
  263.                 exit 0;
  264.         }
  265.  
  266.     &js_title_block($title,$js_main);
  267.     &header_block($title);
  268.  
  269.     %val = %fld;
  270.  
  271.     if (!$number && !$message) { $message = "No existing POP3 accounts."; }
  272.  
  273.     print "<i>$message</i>";
  274.  
  275.     print "<form action=$myname method=post name=POPForm onSubmit=\"return runSubmit()\">\n";
  276.     
  277.     if ($_[0]) { print qq|<input type=hidden name="del_name" value=$_[0]>|; }
  278.  
  279.     print "<center><table width=400>";
  280.  
  281.     print "<tr><td></td><th align=left>Login Name</th></tr>";
  282.  
  283.     print qq|<tr><td><input type=submit name="add" 
  284.     value="Add New Account" onClick="return markAdd()">
  285.     </td><td><input name="new_account" value="$val{'new_account'}" size=20></td></tr>|;
  286.     
  287.     if ($number) {
  288.         undef @locList;
  289.         for ($i=0;$i<$number;$i++) { 
  290.             if ($_[0] ne $name[$i]) { push(@locList,$name[$i]); }
  291.         }
  292.  
  293.         print qq|<tr><td valign=top><input type=submit name="delete" 
  294.         value="Delete Selected Account" onClick="return markDelete()"></td><td>|;    
  295.  
  296.         print &choice_list(*locList,"chosen",20);
  297.  
  298.         print "</td></tr>";
  299.     }
  300.  
  301.     print "</table></center><br>";
  302.  
  303.     print &js_buttons('doit','Ok','onClick="markOther()"','onClick="markOther()"');
  304.  
  305.     print "</form></body></html>";
  306. }
  307.  
  308.